using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using TypeDataset;

namespace MaskUserControl
{
	public delegate void OnOpenMaskDetail(MaskMainView.BaseMaskViewRow maskInfo);
	public partial class MaskGridView : UserControl
	{
		private string _InitFileName = "";
		public event OnOpenMaskDetail OnOpenDetailEvent;
		public MaskGridView()
		{
			InitializeComponent();
		}

		public string XMLFile
		{ 
			get 
			{
				return _InitFileName;
			}
			set 
			{
				_InitFileName = value;
				cardView1.Columns.Clear();
				InitializeFormXML(_InitFileName);
			}
		}
		public MaskMainView.BaseMaskViewDataTable ViewDataSource
		{
			get {
				if (gridControl1.DataSource == null)
					return null;
				return (MaskMainView.BaseMaskViewDataTable)gridControl1.DataSource;
			}
			set {
				gridControl1.DataSource = value;
			}
		}
		public Country.CountryDataTable CountryDataSource
		{
			set {
				MaskCountryLookUpEdit.DataSource = value;
				MaskCountryLookUpEdit.DisplayMember = "country_display";
				MaskCountryLookUpEdit.ValueMember = "country_id";
			}
		}
		public MaskType.MaskTypeDataTable MaskTypeDataSource
		{
			set {
				MaskTypeLookUpEdit.DataSource = value;
				MaskTypeLookUpEdit.DisplayMember = "type_display";
				MaskTypeLookUpEdit.ValueMember = "type_id";
			}
		}

		private void InitializeFormXML(string fileName)
		{
			XPathDocument doc = new XPathDocument(fileName);
			XPathNavigator nav = ((IXPathNavigable)doc).CreateNavigator();
			XPathNodeIterator iter = nav.Select("GridView/MaskGridView");
			iter.MoveNext();
			XPathNodeIterator iterChild = iter.Current.SelectChildren(XPathNodeType.Element);

			while (iterChild.MoveNext())
			{
				string name = iterChild.Current.GetAttribute("Name", "");
				string caption = iterChild.Current.GetAttribute("Caption", "");
				string fieldName = iterChild.Current.GetAttribute("FieldName", "");
				string dataType = iterChild.Current.GetAttribute("DataType", "");
				AddColumn(caption, name, fieldName, dataType);

			}
		}
		private List<DevExpress.XtraGrid.Columns.GridColumn> _ColumnList = new List<DevExpress.XtraGrid.Columns.GridColumn>();
		private void AddColumn(string caption, 
			string name, 
			string fieldName, 
			string dataType)
		{
			DevExpress.XtraGrid.Columns.GridColumn gridColumn = cardView1.Columns.Add();

			_ColumnList.Add(gridColumn);
			gridColumn.OptionsColumn.AllowEdit = false;
			gridColumn.Caption = caption;
			if (dataType == "image")
			{
				gridColumn.ColumnEdit = MaskImageEdit;
			}
			else if (dataType == "lookup")
			{
				if (fieldName == "mask_country")
					gridColumn.ColumnEdit = MaskCountryLookUpEdit;
				else if (fieldName == "mask_type")
					gridColumn.ColumnEdit = MaskTypeLookUpEdit;
				else
					throw new Exception(string.Format("Unknow lookup field: {0}", fieldName));

			}
			else
				gridColumn.ColumnEdit = repositoryItemTextEdit1;

			gridColumn.Name = name;
			gridColumn.FieldName = fieldName;
			gridColumn.Visible = true;
		}

		private void cardView1_DoubleClick(object sender, EventArgs e)
		{
			if (OnOpenDetailEvent != null)
			{
				if (ViewDataSource != null)
				{
					int[] selectIndexes = cardView1.GetSelectedRows();
					if (selectIndexes.Length == 1)
					{
						DataRowView row = (DataRowView)cardView1.GetRow(selectIndexes[0]);
						OnOpenDetailEvent((MaskMainView.BaseMaskViewRow)row.Row);
					}
				}
			}
		}

	}
}
